angular.module('app.services', [])
.provider('router',function($stateProvider, $urlRouterProvider){

    this.$get = function(){}

    this.configRouter = function(){
        $stateProvider
            .state('tab', {
                  url: '/tab',
                  abstract: true,
                  templateUrl: 'templates/tabs.html'
                })
                .state('tab.fx', {
                    url: '/fx',
                    views: {
                      'tab-fx': {
                        templateUrl: 'templates/tab-fx.html',
                        controller: 'FxCtrl'
                    }
                }
            })
            .state('tab.commodity', {
                  url: '/commodity',
                  views: {
                    'tab-commodity': {
                      templateUrl: 'templates/tab-commodity.html',
                      controller: 'CommodityCtrl'
                  }
              }
            })
            .state('tab.index', {
                    url: '/index',
                    views: {
                      'tab-index': {
                        templateUrl: 'templates/tab-index.html',
                        controller: 'IndexCtrl'
                    }
                }
            })
            .state('tab.fx-detail', {
                  url: '/chart/:type/:assetId',
                  views: {
                    'tab-fx': {
                      templateUrl: 'templates/chart-detail.html',
                      controller: 'ChartDetailCtrl'
                  }
              }
            })
            .state('tab.commodity-detail', {
                  url: '/chart/:type/:assetId',
                  views: {
                    'tab-commodity': {
                      templateUrl: 'templates/chart-detail.html',
                      controller: 'ChartDetailCtrl'
                  }
              }
            })
            .state('tab.index-detail', {
                  url: '/chart/:type/:assetId',
                  views: {
                    'tab-index': {
                      templateUrl: 'templates/chart-detail.html',
                      controller: 'ChartDetailCtrl'
                  }
              }
            });

        $urlRouterProvider.otherwise('/tab/fx');
    }

})
.provider('i18n',function($translateProvider){

    this.$get = function($translate){

        return {
            initI18n : function(){
                if(typeof navigator.globalization !== "undefined") {
                    navigator.globalization.getPreferredLanguage(function(language) {
                        $translate.use(language.value.replace("-","_")).then(function(data) {
                            console.log("SUCCESS -> " + data);
                        }, function(error) {
                            console.log("ERROR -> " + error);
                        });
                    }, null);
                }
            }

        }
        
    }

    this.configI18n = function(languages){

        $translateProvider.translations('en_US', languages.en);
        $translateProvider.translations('zh_CN', languages.zh);
        $translateProvider.translations('zh_TW', languages.zh_TW);
        $translateProvider.translations('zh_HK', languages.zh_TW);

        $translateProvider.preferredLanguage("zh_CN");

        $translateProvider.fallbackLanguage("zh_CN");
        
    }

})

.factory('pluginSrv',function(){
    return {
        initPlugin : function(){
            if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
              cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
              cordova.plugins.Keyboard.disableScroll(true);

            }
            if (window.StatusBar && cordova.platformId == 'android') {
                console.log("statusbar....");
              // org.apache.cordova.statusbar required
              //StatusBar.styleLightContent();
              StatusBar.backgroundColorByHexString("#387EF5");
            }
        }

    }
})

.factory('assetSrv',function(){
    return {
        divideAssets : function(assets){
            var assetList = [];
            for(var i=0;i<Math.floor(assets.length / 2);i++){
                assetList.push([assets[i * 2],assets[i * 2 + 1]]);
            }
            if(assets.length % 2 != 0){
                assetList.push([assets[assets.length - 1]]);
            }

            return assetList;
        }

    }
});
